package text111;

import java.awt.PopupMenu;import javax.swing.JInternalFrame;

public class Solution40 {  
	TreeNode res = null ;
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        int pnum = p.val;
        int qnum = q.val;

        if (pnum==root.val||qnum==root.val) {
        	System.out.println("jin");
			res = root;
			System.out.println(res.val);
		}else {
			if (pnum<root.val&&qnum<root.val) {
				lowestCommonAncestor(root.left, p, q);
			}
			if (pnum>root.val&&qnum>root.val) {
				lowestCommonAncestor(root.right, p, q);
			}if ((pnum>root.val&&qnum<root.val)||pnum<root.val&&qnum>root.val) {
				res = root;
			}
		}
		//return res;
		return res;
		
    }

}
